//index.js
import { index } from '../../utils/url.js';
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    searchNum:1,
    adviceItems:[],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    blocklive:[],
    blockheadImg:'',
  },
  //跳转到搜索页面
  tosearch: function() {
      wx.switchTab({
          url: '../search/search'
    })
  },

  onLoad: function () {
      wx.showLoading({
          title: '加载中……',
          icon: 'loading'
      })

      setTimeout(function () {
          wx.hideLoading()
      }, 1000)
      var that = this;
      // 轮播图
      wx.request({
          url: index.swiper,
          method: 'GET',
          header: {
              'content-type': 'application/json'
          },
          success: function (res) {
              that.setData({
                  banner: res.data.result.list
              })
              console.log()

          }
      })
            // 商品分类
        wx.request({
            url: index.Class,
            method: 'GET',
            header: {
                'Accept': 'application/json'
            },
            success: function(res) {
                that.setData({
                    classifyItems: res.data.result.list
                })
                //console.log(res.data.result.list)
               
            }
        })
        // 热销榜
        // wx.request({
        //     url: index.floor,
        //     method: 'GET',
        //     header: {
        //         'Accept': 'application/json'
        //     },
        //     success: function (res) {
        //         that.setData({
        //             floors: res.data.result.list
        //         })
        //     console.log(res.data)
        //     }
        // })
        // 促销商品
        wx.request({
            url: index.floor,
            method: 'GET',
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
            let data = res.data.result.list
            for( let i = 0; i<data.length; i++){                             
                if (data[i].relationGoods.length <= 4){
                        data[i].lookmorehidden = 'hidden';
                    }else{
                       data[i].lookmorehidden = '';
                       data[i].relationGoods.length = 4;
                    }
                    if (data[i].plateUrl == ''){
                        data[i].blockheadImg = 'hidden';
                    }else{
                        data[i].blockheadImg = '';
                    }
               }
               console.log(res.data) 
                that.setData({
                    blocklive: data,
                })

            }
        })
        // 为你推荐
        wx.request({
            url: index.recommend,
            method: 'get',
            data: {
                 searchNum: that.data.searchNum,
            },
            header: {
                'Accept': 'application/json'
            },
            success: function (res) {
                that.setData({
                    adviceItems: res.data.result.list,
                    searchNum: 1,                    
                })
                console.log(res.data)
            }
        })
  },
  onReachBottom: function () {
      var that = this;
      var searchNum = that.data.searchNum + 1;
      var adviceItems = that.data.adviceItems;
      console.log('--------下拉刷新-------')
      wx.showNavigationBarLoading() //在标题栏中显示加载
      wx.request({
          url: index.recommend,
          data: {
              searchNum: searchNum,
          },
          method: 'get',
          success: function (res) {
              for (var i = 0; i < res.data.result.list.length; i++) {
                  adviceItems.push(res.data.result.list[i]);
              }
              console.log(res.data.length);
             // searchNum++;
              that.setData({
                  adviceItems: adviceItems,
                  searchNum: searchNum
              })
          },
          fail: function () {
              console.log('呀！传输失败了哦')
          },
          complete: function () {
              wx.hideNavigationBarLoading() //完成停止加载
              wx.stopPullDownRefresh() //停止下拉刷新
          }
      })
  },
  onShareAppMessage: function (res) {
    var that = this
    // console.log(that.data.teamName)
    if (res.form === 'button') {
      //来自页面内转发按钮
      console.log(res.target);
    }
    return {
      title: "印小七高级班服定制",
      path: '/pages/index/index',
      //imageUrl: 'http://oum84thym.bkt.clouddn.com/association.png',
      success: function (res) {
        //转发成功
        console.log('转发成功');
      },
      fail: function (res) {
        //转发失败
        console.log('转发失败');
      }
    }
  }

})
